<?php
/**
 * Created by PhpStorm.
 * User: sunqiang
 * Date: 15-4-16
 * Time: 上午11:28
 */


class cardType{

    /**
     *代金卡类型查询
     */

    public function search( &$arg_array ) {

        global $G_SHOP;

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();
        //类型ID
        if (isset($arg_array['card_type_id']) && quanli_is_int($arg_array['card_type_id'])) {
            $sql .= ' AND card_type_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['card_type_id'];
        }

        //类型名称
        if (isset($_REQUEST['card_type_name']) && trim($_REQUEST['card_type_name']) != '') {
            $sql .= ' AND card_type_name = ?';
            $params_type .= 's';
            $params_array[] = $_REQUEST['card_type_name'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM card_type ' . $sql;
                $count_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM card_type ' . $sql;

            $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'creator'     	=> $item->creator,
                    'createTime'  	=> $item->createTime,
                    'card_type_id'     => $item->card_type_id,
                    'card_type_name'   => $item->card_type_name

                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }
    }



}
